﻿// (c) Copyright Max Kazarin.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

/// <reference path='../Common/jquery-1.3.2.js' />
// <reference path='../Common/MicrosoftAjax.debug.js' />

Type.registerNamespace('jQueryNET.Controls');

jQueryNET.Controls.ControlBase = function jQueryNET$Controls$ControlBase(element) {
	/// <summary>
	/// This class implement ControlBase control
	/// </summary>
	/// <param name='element' type='Sys.UI.DomElement' domElement='true'>
	/// DOM element the control is associated with
	/// </param>

	jQueryNET.Controls.ControlBase.initializeBase(this, [element]);

	this._rootElement = $(this.get_element());
}

function jQueryNET$Controls$ControlBase$initialize() {
	/// <summary>
	/// Initialize the control
	/// </summary>

	jQueryNET.Controls.ControlBase.callBaseMethod(this, 'initialize');

	this._rootElement.addClass(this.get_theme());
	
	this._rootElement.hover(
		Function.eventWraper(this, this._onMouseOver),
		Function.eventWraper(this, this._onMouseOut));
}

function jQueryNET$Controls$ControlBase$dispose() {
	/// <summary>
	/// Dispose the control
	/// </summary>
	jQueryNET.Controls.ControlBase.callBaseMethod(this, 'dispose');
}

function jQueryNET$Controls$ControlBase$get_width() {
	/// <value type='String'>
	/// Width
	/// </value>

	if (arguments.length !== 0) throw Error.parameterCount();
	return this._rootElement.outerWidth();
}

function jQueryNET$Controls$ControlBase$set_width(value) {
	var e = Function._validateParams(arguments, [{ name: "value", mayBeNull: false}]);
	if (e) throw e;

	this._rootElement.width(value);
	var width = this._rootElement.outerWidth(value);
	if (width > value)
		this._rootElement.width(2 * value - width);
}

function jQueryNET$Controls$ControlBase$get_height() {
	/// <value type='String'>
	/// Height
	/// </value>

	if (arguments.length !== 0) throw Error.parameterCount();
	return this._rootElement.outerHeight();
}

function jQueryNET$Controls$ControlBase$set_height(value) {
	var e = Function._validateParams(arguments, [{ name: "value", mayBeNull: false}]);
	if (e) throw e;

	this._rootElement.height(value);
	var height = this._rootElement.outerHeight(value);
	if (height > value)
		this._rootElement.height(2 * value - height);
}

function jQueryNET$Controls$ControlBase$get_theme() {
	/// <value type='String'>
	/// Control theme class
	/// </value>

	if (arguments.length !== 0) throw Error.parameterCount();

	return this._theme;
}

function jQueryNET$Controls$ControlBase$set_theme(value) {
	var e = Function._validateParams(arguments, [{ name: "value", type: String, mayBeNull: false}]);
	if (e) throw e;

	this._theme = value;
	this._rootElement.addClass(this._theme);
}

function jQueryNET$Controls$ControlBase$_onMouseOver() {
	this.raiseActive();
}

function jQueryNET$Controls$ControlBase$_onMouseOut() {
	this.raiseDeactive();
}

function jQueryNET$Data$DataSourceBase$add_active(handler) {
	/// <summary>
	/// Add an event handler for the active event
	/// </summary>
	/// <param name='handler' type='Function' mayBeNull='false'>
	/// Event handler
	/// </param>
	/// <returns />

	this.get_events().addHandler('active', handler);
}

function jQueryNET$Data$DataSourceBase$remove_active(handler) {
	/// <summary>
	/// Remove an event handler from the active event
	/// </summary>
	/// <param name='handler' type='Function' mayBeNull='false'>
	/// Event handler
	/// </param>
	/// <returns />

	this.get_events().removeHandler('active', handler);
}

function jQueryNET$Data$DataSourceBase$raiseActive(eventArgs) {
	/// <summary>
	/// Raise the active event
	/// </summary>
	/// <param name='eventArgs' type='Sys.CancelEventArgs' mayBeNull='false'>
	/// Event arguments for the click event
	/// </param>
	/// <returns />

	var handler = this.get_events().getHandler('active');
	if (handler) {
		handler(this, eventArgs);
	}
}

function jQueryNET$Data$DataSourceBase$add_deactive(handler) {
	/// <summary>
	/// Add an event handler for the deactive event
	/// </summary>
	/// <param name='handler' type='Function' mayBeNull='false'>
	/// Event handler
	/// </param>
	/// <returns />

	this.get_events().addHandler('deactive', handler);
}

function jQueryNET$Data$DataSourceBase$remove_deactive(handler) {
	/// <summary>
	/// Remove an event handler from the deactive event
	/// </summary>
	/// <param name='handler' type='Function' mayBeNull='false'>
	/// Event handler
	/// </param>
	/// <returns />

	this.get_events().removeHandler('deactive', handler);
}

function jQueryNET$Data$DataSourceBase$raiseDeactive(eventArgs) {
	/// <summary>
	/// Raise the deactive event
	/// </summary>
	/// <param name='eventArgs' type='Sys.CancelEventArgs' mayBeNull='false'>
	/// Event arguments for the click event
	/// </param>
	/// <returns />

	var handler = this.get_events().getHandler('deactive');
	if (handler) {
		handler(this, eventArgs);
	}
}

jQueryNET.Controls.ControlBase.prototype = {
	_rootElement: null,
	_theme: 'default',

	initialize: jQueryNET$Controls$ControlBase$initialize,
	dispose: jQueryNET$Controls$ControlBase$dispose,

	get_width: jQueryNET$Controls$ControlBase$get_width,
	set_width: jQueryNET$Controls$ControlBase$set_width,
	get_height: jQueryNET$Controls$ControlBase$get_height,
	set_height: jQueryNET$Controls$ControlBase$set_height,
	get_theme: jQueryNET$Controls$ControlBase$get_theme,
	set_theme: jQueryNET$Controls$ControlBase$set_theme,
	
	_onMouseOver: jQueryNET$Controls$ControlBase$_onMouseOver,
	_onMouseOut: jQueryNET$Controls$ControlBase$_onMouseOut,	

	add_active: jQueryNET$Data$DataSourceBase$add_active,
	remove_active: jQueryNET$Data$DataSourceBase$remove_active,
	raiseActive: jQueryNET$Data$DataSourceBase$raiseActive,
	add_deactive: jQueryNET$Data$DataSourceBase$add_deactive,
	remove_deactive: jQueryNET$Data$DataSourceBase$remove_deactive,
	raiseDeactive: jQueryNET$Data$DataSourceBase$raiseDeactive	
}

jQueryNET.Controls.ControlBase.registerClass('jQueryNET.Controls.ControlBase', AjaxControlToolkit.ControlBase);
